package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.page.FeedbackQuery;
import com.htgd.gardenexpo.page.InformationQuery;
import com.htgd.gardenexpo.service.GeMyInformationService;
import com.htgd.gardenexpo.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 常用信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-21
 */
@RestController
@RequestMapping("/api/information")
@Slf4j
@Api(tags = "常用信息")
public class GeMyInformationController {

    @Autowired
    private GeMyInformationService geMyInformationService;


    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add (@RequestBody GeMyInformation geMyInformation,
                                         HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);

        log.info("常用信息-添加：{},{}",geMyInformation,openId);
        geMyInformation.setOpenId(openId);
        geMyInformation.setDeleteFlag("0");
        geMyInformationService.save(geMyInformation);
        return CommonResult.success("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit (@RequestBody GeMyInformation geMyInformation){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geMyInformationService.updateById(geMyInformation);
            result.setCode(200);
            result.setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete (int id){
        log.info("常用信息-删除：{}",id);
        return geMyInformationService.delete(id);
    }

    @PostMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeMyInformation>> list(@RequestBody InformationQuery informationQuery,
                                                    HttpServletRequest request) {
        CommonResult<List<GeMyInformation>> result = new CommonResult<>();
        try {
            String accessToken = request.getHeader("Access_token");
            String openId = TokenUtil.getUsernameFromToken(accessToken);
            PageInfo<GeMyInformation> list=geMyInformationService.querylist(informationQuery,openId);
            result.setCode(200);
            result.setData(list.getList());
            result.setMessage(String.valueOf(list.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeMyInformation> details(Integer id) {
        CommonResult<GeMyInformation> result = new CommonResult<>();
        try {
            GeMyInformation geMyInformation=geMyInformationService.getById(id);
            geMyInformation.setOpenId("");
            result.setCode(200);
            result.setData(geMyInformation);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }
}
